package com.winz.cultureapp.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.StadiumDesBean;
import com.winz.cultureapp.entity.StadiumDesListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 场地详情
 */
public class StadiumListDesActivity extends BaseAppCompatActivity {

    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.iv_right)
    ImageView mIv_right;

    @BindView(R.id.wv_content)
    WebView mWv_content;
    @BindView(R.id.progress)
    ProgressBar mProgress;
    @BindView(R.id.tv_order)
    TextView mTv_order;

    private String href;
    private Bundle mBd_data;

    private int unitType; // 0 -- 固定场馆   1-- 非固定
    private int bookStatus; // 0--不可预定 1--可预定
    private String unitName;

    @Override
    protected int contentView() {
        return R.layout.activity_stadium_lsit_des;
    }

    @Override
    protected void initViews() {
        mIv_right.setVisibility(View.VISIBLE);
        mIv_right.setImageResource(R.drawable.icon_share);
        mTv_top_title.setText("场馆详情");

        mWv_content.getSettings().setJavaScriptEnabled(true);//设置WebView支持JS
        mWv_content.getSettings().setDefaultTextEncodingName("utf-8");//设置解码时的字符集
        mWv_content.getSettings().setLoadWithOverviewMode(true);
        mWv_content.getSettings().setUseWideViewPort(true);
        mWv_content.getSettings().setSupportZoom(true);
        mWv_content.getSettings().setBuiltInZoomControls(true);
        mWv_content.getSettings().setDomStorageEnabled(true);
        mWv_content.getSettings().setBuiltInZoomControls(false);
        mWv_content.setWebViewClient(new WebViewClient() {
            /*
            此处能拦截超链接的url,即拦截href请求的内容.
            */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
            }

        });
        mWv_content.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    mProgress.setVisibility(View.GONE);//加载完网页进度条消失
                } else{
                    mProgress.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mProgress.setProgress(newProgress);//设置进度值

                }
            }
        });
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null) {
            mBd_data = intent.getExtras();
            if (intent.getExtras() != null) {
                href = intent.getExtras().getString(Constants.KEY.STADIUM.ITEM_DES_HREF);
                unitType = intent.getExtras().getInt(Constants.KEY.STADIUM.ITEM_UNIT_TYPE);
                bookStatus = intent.getExtras().getInt(Constants.KEY.STADIUM.ITEM_BOOK_STATUS);
                unitName = intent.getExtras().getString(Constants.KEY.STADIUM.ITEM_TITLE);

                mWv_content.loadUrl(href);

                if (bookStatus == 1){
                    mTv_order.setClickable(true);
                    mTv_order.setBackgroundColor(getResources().getColor(R.color.base_color));
                }else {
                    mTv_order.setClickable(false);
                    mTv_order.setBackgroundColor(getResources().getColor(R.color.unchecked_color));
                }
            }
        }
    }

    @OnClick({R.id.fl_back, R.id.rl_right, R.id.tv_order})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.rl_right:

                break;

            case R.id.tv_order: //预定
                if (unitType == 0){
                    // 固定
                    startActivity(BookStadiumActivity.class, false, mBd_data,0);
                }else if(unitType ==1){
                    // 非固定
                    startActivity(BookStadium_NoActivity.class,false,mBd_data);
                }

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK){
            setResult(RESULT_OK);
            finish();
        }
    }


}
